/**
 * 回复评论页面，
 * author:caiyl5
 * date:2016年9月2日13:54:22
 */
(function(){
	var rebackCommentDetail = {
		/**
		 * bindEvent 页面事件
		 */
		bindEvent:function(){
			//获取窗口索引
			var index = parent.layer.getFrameIndex(window.name); 
			//回复评论按钮点击事件
            $(".send-btn").unbind().on("click", function () {
            	var comment_contant = $(".reply-people").val();
				if (comment_contant.length<5) {
					layer.msg('您回复的内容少于5个字');
				} else {
					var param={
						"comment_contant":comment_contant,
						"pk_user":window.localStorage.userid,
						"pk_publish":window.sessionStorage.rebackComment_publish,
						"pk_parent_comment":$(".reply-people").attr("data-id"),
						"pk_parent_user":$(".reply-people").attr("data-user")
					};
					//console.log(param);
					$(".send-btn").attr("disabled",true);
					rebackCommentDetail.answerComment(param);
				}

            });
		},
		/**
		 * 初始化知识阅读内容
		 */
		initReading:function(){
			var param ={
				"pk_user":	window.localStorage.userid,
				"pk_publish":window.sessionStorage.rebackComment_publish
			}
			//转圈
			common.showLoading();
			var url = "/kmpublish/App/queryKnowledgeAttByPK";
			common.interfaceAjaxFunction(url,param,this.queryKnowledgeAttByPKSuccess);
		},
		/**
		 * 初始化知识阅读成功回调函数
		 */
		queryKnowledgeAttByPKSuccess:function(res){
			common.hideLoading();
			//console.log(res);
			//console.log("调用接口返回JSON数据串为：" + JSON.stringify(res));
			if(res.status== "0"){
				var data = res.data[0];
				$(".knowledgetilehead").text(data.publish_title);

					//console.log(data.publish_year);
				//时间
				if (!data.publish_year||!data.publish_month||!data.publish_day) {
					$("#knowledge_time").html("未知");
				} else {
					var publish_time = data.publish_year+"."+data.publish_month+"."+data.publish_day;
					$("#knowledge_time").text(publish_time);
				}
				if (data.publish_user_name) {
					$("#knowledge_author").text(data.publish_user_name);
				} else {
					$("#knowledge_author").text("佚名");
				}
				// $("#knowledge_author").text(data.publish_user_name);
			    $(".knowledgetilekeycon").text(data.publish_memo);
				

				//关键字
			    var keys = data.publish_key.split(",");
				var html = "";
				for (var i=0; i < keys.length-1; i++) {
					html +=keys[i]+",";
				};
				html +=keys[i];
				$("#knowledge_key").html(html);

				if (data.achementList_pic &&data.achementList_pic.length>0) {
					 $(".knowledgetilepicture").append(" ");
					for (var i=0; i < data.achementList_pic.length; i++) {
					 var pic = data.achementList_pic[i];
					 //下载制定的文件
					 rebackCommentDetail.picDownload(pic.pk_attachment,data.achementList_pic.length);
					}
				}
				
				//文件
				if (data.achementList_oth &&data.achementList_oth.length>0) {
					 $(".knowledgetilepicdescbe").html("<span>文档</span>");
					  var ohtml="";
					for (var i=0; i < data.achementList_oth.length; i++) {	
					  var att = data.achementList_oth[i];
					  // var publishtime = att.att_year+"."+att.att_month+"."+att.att_day;
					  ohtml += '<p class="achementList_commentDetail" data-id="'+att.pk_attachment+'"><a class="blue-color waitcallback-content comment_contant" href="'+att.fileurl+'" >'+att.fname+"("+att.filesize+")"+'</a></p>';  
                       
					};
					$(".knowledgetilepicdescbe").append(ohtml);
					//事件监听
					$(".achementList_commentDetail").unbind().on("click",function(){
						var dataId = $(this).attr("data-id");
						//请求参数
						var url = "/file/downloadFile/" + dataId;
						//通过iframe返回到企业+前一页面
    					var $body = $('body');
    					var $iframe = $('<iframe id="title_iframe_commentDetail" src="" style="border:none;width:0px;height:0px;"></iframe>');
    			        $iframe.appendTo($body);
    			        document.getElementById("title_iframe_commentDetail").src = common.contextPath() + url + "?radom=" + Math.random();
					});
				}
				
				//初始化评论列表
				rebackCommentDetail.initComment();
			}else{
				common.showTip('调用接口失败，请检查网络后重试');
			}
		},
		/**
		 * 图片的异步取得
		 */
		picDownload:function(filePk,photoLength){
			 //请求参数
			 var url = "/file/picdownload/" + filePk;
			 common.interfaceAjaxFunction(url,"",rebackCommentDetail.picDownloadSuccess,true,photoLength,false);
		},
		/**
		 * 下载成功
		 */
		picDownloadSuccess:function(res,photoLength){
			 var ohtml = '<p><img src="data:image/png;base64,'+res+'" alt=""/></p>';
			 $(".knowledgetilepicture").append(ohtml);
			//图片预览
			 if($(".knowledgetilepicture img").length == photoLength){
				 layer.ready(function(){ //为了layer.ext.js加载完毕再执行
					  layer.photos({
					    photos: '#knowledget_layer_phote_commentDetail'
					  });
					}); 
			 }
		},
		/**
		 * 初始化评论
		 */
		initComment:function(){
			common.showLoading();
			//初始化输入框
			$("#comment_content").val("");
			var param={
				"pk_publish":window.sessionStorage.rebackComment_publish,
				"pk_user":window.localStorage.userid,
				"pageSize":common.pageSize,
				"pageNum":common.currentPage,
				"onlyOne":"1",
				"pk_comment":window.sessionStorage.rebackComment_comment
			};
			//请求参数
			var url = "/pc/comment/queryComment";
			common.interfaceAjaxFunction(url,param,rebackCommentDetail.queryCommentListSuccess);

		},
		/**
		 * 
		 * 初始化评论成功回调
		 */
		queryCommentListSuccess:function(res){
			//成功时候
	    	if(res.status == "0"){
	    		//console.log(res);
				var user_name = res.data[0].user_name;
				if (user_name == undefined || user_name == "") {
					user_name = "匿名"
				}
				var comment_content = res.data[0].comment_contant;
				var comment_time = res.data[0].comment_time;
				var pk_user = res.data[0].pk_user;
				var pk_comment = res.data[0].pk_comment;

				$("#comment").text(user_name+"："+comment_content);
				$("#comment_time").text(comment_time);
				$(".reply-people").attr("data-id",pk_comment);
				$(".reply-people").attr("data-user",pk_user);
	    	}else{
	    		//给出信息提示
	    		common.show_error(res.message);
	    	}
	    	//gund
			common.hideLoading();
			//滚动条到底部
			window.scroll(0,document.body.scrollHeight);
			//绑定点击事件
			rebackCommentDetail.bindEvent();
		},
		/**
		 * 回复评论、、
		 */
		answerComment:function(res){
			var url = "/pc/comment/answer";
			//console.log(res);
			common.interfaceAjaxFunction(url,res,this.answerCommentSuccess);
		},
		/**
		 * 回复评论成功回调、、
		 */
		answerCommentSuccess:function(res){
			//成功时候
	    	if(res.status == "0"){
	    		//console.log(res);
				var index = parent.layer.getFrameIndex(window.name);
				//刷新父界面
				parent.$(".reply-comment-title").click();
				//关闭弹出窗
				parent.layer.close(index);
				parent.layer.msg("回复评论成功");
	    	}else{
	    		//给出信息提示
	    		common.show_error(res.message);
	    	}
			
		}
	};
	$(document).ready(function(){
		//绑定事件
		rebackCommentDetail.bindEvent();
		//初始化知识阅读界面
		rebackCommentDetail.initReading();
	});
})();
